package com.demo.test;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @ClassName: TwoListCopare
 * @Description: 两个List<对象>取交集\并集\差集</>
 **/
public class TwoListString {
    public static void main(String[] args) {
        UserDTO userOld1 = new UserDTO("1","aaa",22);
        UserDTO userOld2 = new UserDTO("2","bbb",32);
        UserDTO userOld3 = new UserDTO("3","ccc",11);
        UserDTO userOld4 = new UserDTO("4","ddd",42);
        UserDTO userOld5 = new UserDTO("5","bbb",22);
        UserDTO userOld6 = new UserDTO("6","eee",24);

        UserDTO userNew1 = new UserDTO("7","dada",22);   //新增一个
        UserDTO userNew2 = new UserDTO("2","bbb",32);   //不变一个
        UserDTO userNew3 = new UserDTO("3","kaka",33);   //更新一个
        UserDTO userNew4 = new UserDTO("8","dbdb",42);   //新增一个
        UserDTO userNew5 = new UserDTO("5","bbb",100);   //更新一个
        //当然，少了1,4,6



        List<String> oldIds = new ArrayList<>();
        String a = "1";
        String b = "2";
        String c = "3";
        String d = "4";
        //添加老数据
        oldIds.add(a);
        oldIds.add(b);
        oldIds.add(c);
        oldIds.add(d);
        List<String> newIds = new ArrayList<>();
        String e = "1";
        String f = "2";
        String g = "5";

        //添加新数据
        newIds.add(e);
        newIds.add(f);
        newIds.add(g);


        //去交集,既获取id相同的交集，需要更新
        //1.先提取出id和结果，用map形式
        //List<String> oldIds = new ArrayList<>();
        //List<String> newIds = new ArrayList<>();
       /* oldList.stream().forEach(it->oldIds.add(it.getId()));
        newList.stream().forEach(it->newIds.add(it.getId()));*/
//    oldIds.stream().forEach(System.out::println);
//    newIds.stream().forEach(System.out::println);

        //取交集id
        System.out.println("-----------------交集----------------------");
        List<String> collectUpdate = newIds.stream().filter(it -> oldIds.contains(it)).collect(Collectors.toList());
        collectUpdate.stream().forEach(System.out::println);

        //取old的差集
        System.out.println("-----------------old的差集----------------------");
        List<String> collectDelete = oldIds.stream().filter(it -> !newIds.contains(it)).collect(Collectors.toList());
        collectDelete.stream().forEach((System.out::println));

        //取new的差集
        System.out.println("-----------------new的差集----------------------");
        List<String> collectAdd = newIds.stream().filter(it -> !oldIds.contains(it)).collect(Collectors.toList());
        collectAdd.stream().forEach((System.out::println));

        //取并集
        System.out.println("-------------------并集--------------------");
        List<String> allIds = new ArrayList<>();
        //获取一个包含了oldIds和newIds的总结合,但是没有去重
        allIds.addAll(oldIds);
        allIds.addAll(newIds);
        //去重，获取并集ids的新集合
        List<String> joinIds = allIds.stream().distinct().collect(Collectors.toList());
        joinIds.stream().forEach(System.out::println);
    }
}